<?php

/* * *********************************************************** */
/* * ********************** Classe UsuarioDao ********************* */
/* * ************ Desenvolvido por Antônio Ezequiel ************** */
/* * *********************************************************** */

class UsuarioDao extends Usuario {

    public function adicionarUsuario() {

        global $configurator;

        parent::conectarBD();
        
        $queri = parent::query("insert into tbl_usuario (usu_nome, usu_login, usu_email, usu_senha, usu_cpf, usu_rg, usu_cep, usu_bairro,
                                usu_rua, usu_numero, usu_complemento, usu_ativo, usu_cel, usu_fone, usu_troca_senha, usu_ocu_codigo, usu_data_cadastro)
                                values ('$this->usu_nome','$this->usu_login', '$this->usu_email','$this->usu_senha','$this->usu_cpf',
                                '$this->usu_rg', '$this->usu_cep', '$this->usu_bairro', '$this->usu_rua', '$this->usu_numero', '$this->usu_complemento',
                                '$this->usu_ativo', '$this->usu_cel', '$this->usu_fone', '$this->usu_troca_senha', '$this->usu_ocu_codigo', now())");

        if ($queri) {
            $id = parent::get_insert_id();
            parent::desconectarBD();
            return $id;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function logarUsuario() {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select * from tbl_usuario where usu_login = '$this->usu_login' and usu_senha = '$this->usu_senha'");

        if ($queri) {
            $dados = $queri->fetch_assoc();
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarUsuariosAutoComplite($nome) {

        global $configurator;

        parent::conectarBD();
        
        $queri = parent::query("select usu_nome as nome, usu_codigo as codigo from tbl_usuario where usu_nome like '%$nome%' order by usu_nome asc");

        if ($queri) {
            $dados = parent::fetch_rows($queri);
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function atualizarUsuario($codigo) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("update tbl_usuario set usu_nome = '$this->usu_nome', usu_login = '$this->usu_login', usu_email = '$this->usu_email',
                                usu_cpf = '$this->usu_cpf', usu_rg = '$this->usu_rg', usu_cep = '$this->usu_cep', usu_bairro = '$this->usu_bairro',
                                usu_rua = '$this->usu_rua', usu_numero = '$this->usu_numero', usu_complemento = '$this->usu_complemento',
                                usu_cel = '$this->usu_cel', usu_fone = '$this->usu_fone', usu_ocu_codigo = '$this->usu_ocu_codigo'
                                where usu_codigo = '$codigo'");

        if ($queri) {
            parent::desconectarBD();
            return true;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function atualizarUsuarioSenha($codigo) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("update tbl_usuario set usu_senha = '$this->usu_senha', usu_troca_senha = 0 where usu_codigo = '$codigo'");

        if ($queri) {
            parent::desconectarBD();
            return true;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarUsuarioNome($nome) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select usu_codigo as codigo from tbl_usuario where usu_nome like '$nome'");

        if ($queri) {
            $dados = $queri->fetch_assoc();
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function deletarUsuario($codigo) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("update tbl_usuario set usu_ativo = '0' where usu_codigo = '$codigo'");

        if ($queri) {
            parent::desconectarBD();
            return true;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarUsuariosPaginado($inicio, $porPagina) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select usu_nome as nome, usu_login as login, usu_codigo as codigo, ocu_ocupacao as ocupacao, usu_ultimo_log as log from tbl_usuario u inner join tbl_ocupacao o on
                                u.usu_ocu_codigo = o.ocu_codigo  where usu_ativo = '1' limit $inicio, $porPagina");

        if ($queri) {
            $dados = parent::fetch_rows($queri);
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarUsuariosPaginadoAjax($ini, $codigo, $inicio, $porPagina) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query(sprintf("select usu_nome as nome, usu_login as login, usu_codigo as codigo, usu_ultimo_log as log, ocu_ocupacao as ocupacao from tbl_usuario
                                        inner join tbl_ocupacao on usu_ocu_codigo = ocu_codigo
                                        where %s %s %s %s %s %s LIMIT $inicio, $porPagina", $ini, $codigo, $this->usu_nome,
                                $this->usu_ocu_codigo, $this->usu_rg, $this->usu_cpf));

        if ($queri) {
            $dados = parent::fetch_rows($queri);
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function listarUsuario($codigo) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select usu_nome as nome, usu_login as login, usu_codigo as codigo, usu_rua as rua, usu_email as email,
                                usu_cpf as cpf, usu_rg as rg, usu_cep as cep, usu_bairro as bairro, usu_numero as numero,
                                usu_complemento as complemento, usu_cel as cel, usu_fone as fone, usu_ocu_codigo as ocupacao
                                from tbl_usuario where usu_codigo ='$codigo' and usu_ativo = '1'");

        if ($queri) {
            $dados = $queri->fetch_assoc();
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function totalPaginas() {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select count(usu_codigo) as quantidade from tbl_usuario where usu_ativo = '1'");

        if ($queri) {
            $dados = $queri->fetch_assoc();
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function totalPaginasAjax($ini, $codigo) {

        global $configurator;

        parent::conectarBD();
        
        $queri = parent::query(sprintf("select count(*) as totRegistros from tbl_usuario
                                        inner join tbl_ocupacao on usu_ocu_codigo = ocu_codigo
                                        where %s %s %s %s %s %s", $ini, $codigo, $this->usu_nome,
                                $this->usu_ocu_codigo, $this->usu_rg, $this->usu_cpf));

        if ($queri) {
            $dados = $queri->fetch_assoc();
            parent::desconectarBD();
            return $dados;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function atualizaTempoUsuario($codigo) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("update tbl_usuario set usu_ultimo_log = now() where usu_codigo = '$codigo'");

        if ($queri) {
            parent::desconectarBD();
            return true;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function verificaCpf($cpf) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select usu_codigo as codigo from tbl_usuario where usu_cpf = '$cpf'");

        if ($queri) {
            $dados = $queri->fetch_assoc();
            parent::desconectarBD();
            if($dados[codigo])
                return true;
            else
                return false;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

    public function verificaLogin($login) {

        global $configurator;

        parent::conectarBD();

        $queri = parent::query("select usu_codigo as codigo from tbl_usuario where usu_login = '$login'");

        if ($queri) {
            $dados = $queri->fetch_assoc();
            parent::desconectarBD();
            if($dados[codigo])
                return true;
            else
                return false;
        } else {
            parent::desconectarBD();
            return false;
        }
    }

}

?>
